package com.handle.common.log.listen;

import com.handle.common.log.event.SysLoginLogEvent;
import com.handle.common.log.event.SysOperateLogEvent;
import com.handle.system.api.entity.SysLoginLog;
import com.handle.system.api.entity.SysOperateLog;
import com.handle.system.api.feign.RemoteSystemLoginLog;
import com.handle.system.api.feign.RemoteSystemOperateLog;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

/**
 * 异步监听日志事件
 * @author zhangqing
 */
@Slf4j
@AllArgsConstructor
public class LogListener {

    private final RemoteSystemLoginLog systemLoginLog;
    private final RemoteSystemOperateLog systemOperateLog;

    @Async
    @Order
    @EventListener(SysLoginLogEvent.class)
    public void listenLoginLog(SysLoginLogEvent event) {
        SysLoginLog sysLoginLog = (SysLoginLog) event.getSource();
        log.info("远程访问日志记录成功：{}", sysLoginLog);
        systemLoginLog.addLoginLog(sysLoginLog);
    }

    @Async
    @Order
    @EventListener(SysOperateLogEvent.class)
    public void listenOperateLog(SysOperateLogEvent event) throws Exception{
        SysOperateLog sysOperateLog =  (SysOperateLog)event.getSource();
        log.info("远程操作日志记录成功：{}", sysOperateLog);
        systemOperateLog.addOperateLog(sysOperateLog);
    }
}
